#include "Enemy.h"

Enemy::Enemy () : mActive (false), mDelay (0.0f) //remind Peter to expand on this
{
	
}
Enemy::~Enemy ()
{
	
}

void Enemy::Spawn (const SVector2& pos)
{
	mPosition = pos;
	mVelocity = SVector2 (RandomFloat (-200.0f, 200.0f), 200.0f);
	mDelay = RandomFloat (0.1f, 4.0f);
	mActive = true;
}
void Enemy::Kill ()
{
	mActive = false;
}

void Enemy::Load ()
{
	mSprite.Load ("pikachu.png");
}
void Enemy::Render ()
{
	if (mActive)
	{
		const int width = mSprite.GetWidth ();
		const int height = mSprite.GetHeight ();
		SVector2 offset ((width * 0.5f), (height * 0.5f));
		SVector2 renderPosition = mPosition - offset;

		mSprite.SetPosition (renderPosition);
		mSprite.Render ();
	}
}
void Enemy::Unload ()
{
	mSprite.Unload ();
}
void Enemy::Update (float deltaTime)
{
	if (mActive)
	{
		mPosition += mVelocity * deltaTime;

		//sets width and height based on window size added to size of sprite, or else defaults to 800x600
		const int kWinWidth = IniFile_GetInt ("WinWidth", 800);
		const int kWinHeight = IniFile_GetInt ("WinHeight", 600);
		const int kThreshold = mSprite.GetWidth ();

		//if the enemey is off the screen, kill the enemy
		if (mPosition.x < -kThreshold || mPosition.x > (kWinWidth + kThreshold) || mPosition.y < -kThreshold ||mPosition.y > (kWinHeight + kThreshold))
		{
			Kill ();
		}

		//creates a delay that decrements with time
		mDelay -= deltaTime;

		//checks to see if the delay is equalivalant or less than 0
		if (mDelay <= 0)
		{
			//reverses the movement direction
			mVelocity.x *= -1.0f;

			//sets delay to a random float value between 0.1 and 4.0
			mDelay = RandomFloat (0.1f, 4.0f); 
		}
	}

	
}